package com.android.fitcontrol;

import java.util.LinkedList;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

import com.android.fitcontrol.bean.Many;

public class DetailListAdapter extends SimpleCursorAdapter{
	
	private LinkedList<Many> list;
	private boolean isDataChanged = false;
	
	public DetailListAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		list = new LinkedList<Many>();
		fillList(c);
		isDataChanged = false;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = super.getView(position, convertView, parent);
		final int pos = position;
		final EditText plannedCount = (EditText) view.findViewById(R.id.planned_count_edit_text);
		final EditText plannedWeight = (EditText) view.findViewById(R.id.planned_weight_edit_text);
		final EditText doneCount = (EditText) view.findViewById(R.id.done_count_edit_text);
		final EditText doneWeight = (EditText) view.findViewById(R.id.done_weight_edit_text);
		plannedCount.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus && plannedCount.getText().length() > 0){
					Many set = list.get(pos);
					if(set.getPlannedCount() != Integer.valueOf(plannedCount.getText().toString())){
						set.setPlannedCount(Integer.valueOf(plannedCount.getText().toString()));
						isDataChanged = true;
					}
				}
			}
		});
		
		plannedWeight.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus && plannedWeight.getText().length() > 0){
					Many set = list.get(pos);
					if(set.getPlannedWeight() != Float.valueOf(plannedWeight.getText().toString())){
						set.setPlannedWeight(Float.valueOf(plannedWeight.getText().toString()));
						isDataChanged = true;
					}
				}
			}
		});
		
		doneWeight.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus && doneWeight.getText().length() > 0){
					Many set = list.get(pos);
					if(set.getDoneWeight() != Float.valueOf(doneWeight.getText().toString())){
						set.setDoneWeight(Float.valueOf(doneWeight.getText().toString()));
						isDataChanged = true;
					}
				}
			}
		});
		
		doneCount.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(!hasFocus && doneCount.getText().length() > 0){
					Many set = list.get(pos);
					if(set.getDoneCount() != Integer.valueOf(doneCount.getText().toString())){
						set.setDoneCount(Integer.valueOf(doneCount.getText().toString()));
						isDataChanged = true;
					}
				}
			}
		});
		
		return view;
	}
	
	@Override
	public void changeCursor(Cursor cursor) {
		// TODO Auto-generated method stub
		super.changeCursor(cursor);
		fillList(cursor);
		isDataChanged = false;
	}
	
	public Many getManyAtPosition(int position){
		return list.get(position);
	}
	
	public boolean isModified(){
		return isDataChanged;
	}
	
	private void fillList(Cursor c){
		list = new LinkedList<Many>();
		Many set = new Many();
		if(c.moveToFirst()){
			set.set_ID(c.getInt(Many.NUM_ID));
			set.setPlannedCount(c.getInt(Many.NUM_PLANNED_COUNT));
			set.setPlannedWeight(c.getFloat(Many.NUM_PLANNED_WEIGHT));
			set.setDoneCount(c.getInt(Many.NUM_DONE_COUNT));
			set.setDoneWeight(c.getFloat(Many.NUM_DONE_WEIGHT));
			set.setExercise_ID(c.getInt(Many.NUM_EXERCISE_ID));
			list.add(set);
		}
		while(c.moveToNext()){
			set = new Many();
			set.set_ID(c.getInt(Many.NUM_ID));
			set.setPlannedCount(c.getInt(Many.NUM_PLANNED_COUNT));
			set.setPlannedWeight(c.getFloat(Many.NUM_PLANNED_WEIGHT));
			set.setDoneCount(c.getInt(Many.NUM_DONE_COUNT));
			set.setDoneWeight(c.getFloat(Many.NUM_DONE_WEIGHT));
			set.setExercise_ID(c.getInt(Many.NUM_EXERCISE_ID));
			list.add(set);
		}
	}
}
